/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package l1j.server.server.clientpackets;

import l1j.server.Config;
import l1j.server.server.ClientThread;
import l1j.server.server.model.L1World;
import l1j.server.server.model.Instance.L1DollInstance;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.Instance.L1PetInstance;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.utils.LogRecorder;
//add 防止複製道具 code (1/2) 
import l1j.server.extra.L1CheckPcItem; 
// add end

/**
 * 處理收到由客戶端傳來丟道具到地上的封包
 */
public class C_DropItem extends ClientBasePacket {
	private static final String C_DROP_ITEM = "[C] C_DropItem";

	public C_DropItem(byte[] decrypt, ClientThread client) throws Exception {
		super(decrypt);
		int x = readH();
		int y = readH();
		int objectId = readD();
		int count = readD();
//		防洗物品
		count = Math.abs(count);
		count = Math.min(count,2000000000);
		//防洗物品 end
		L1PcInstance pc = client.getActiveChar();
		if (pc.isGhost()) {
			return;
		} else if (pc.getMapId() >= 16384 && pc.getMapId() <= 25088) { // 旅館內判斷
			pc.sendPackets(new S_ServerMessage(539)); // \f1你無法將它放在這。
			return;
		}

		L1ItemInstance item = pc.getInventory().getItem(objectId);
		if (item != null) {
			if (!item.getItem().isTradable()) {
				// \f1%0%d是不可轉移的…
				pc.sendPackets(new S_ServerMessage(210, item.getItem()
						.getName()));
				return;
			}

			// 使用中的寵物項鍊 - 無法丟棄
					for (L1NpcInstance petNpc : pc.getPetList().values()) {
						if (petNpc instanceof L1PetInstance) {
						L1PetInstance pet = (L1PetInstance) petNpc;
					if (item.getId() == pet.getItemObjId()) {
						pc.sendPackets(new S_ServerMessage(1187)); // 寵物項鍊正在使用中。
						return;
					}
				}
			}
			// 使用中的魔法娃娃 - 無法丟棄
					for (L1DollInstance doll : pc.getDollList().values()) {
						if (doll.getItemObjId() == item.getId()) {
						pc.sendPackets(new S_ServerMessage(1181)); // 這個魔法娃娃目前正在使用中。
						return;
				}
			}
//		      物品丟地上系統會自動刪除 by bill00148 
			if (pc.isGm() || item.getItemId() == 40010 || // 治癒藥水
					item.getItemId() == 40011 ||          // 強力治癒藥水
					item.getItemId() == 40012 ||          // 終極治癒藥水
					item.getItemId() == 40013 ||          // 自我加速藥水
					item.getItemId() == 40056 ||          // 肉
					item.getItemId() == 40057 ||          // 漂浮之眼肉
					item.getItemId() == 40060 ||          // 胡蘿蔔
					item.getItemId() == 40070 ||          // 進化果實
					item.getItemId() == 40725 ||          // 南瓜糖果
					item.getItemId() == 40726 ||          // 南瓜種子
					item.getItemId() == 88 ||             // 潘的角
					item.getItemId() == 40045 ||          // 紅寶石
					item.getItemId() == 40494 ||          // 純粹的米索莉塊
					item.getItemId() == 40495 ||          // 米索莉線
					item.getItemId() == 40520 ||          // 精靈粉末
					item.getItemId() == 40521 ||          // 精靈羽翼
					item.getItemId() == 40508 ||          // 奧裡哈魯根
					item.getItemId() == 40493 ||          // 魔法笛子
					item.getItemId() == 40032 ||          // 伊娃的祝福
					item.getItemId() == 41310) {          // 勝利果實
		                }
			else if (pc.getLevel() <= 99){
            pc.getInventory().removeItem(objectId,count); 
            pc.sendPackets(new S_SystemMessage("物品請勿丟地上，已遭系統刪除。"));
            return;
            }
//	      物品丟地上系統會自動刪除 by bill00148
//			 add 防止複製道具 code (2/2) 
			L1CheckPcItem checkPcItem = new L1CheckPcItem(); 
			boolean isCheat = checkPcItem.checkPcItem(item, pc); 
			if (isCheat) { 
			L1World.getInstance().broadcastServerMessage("\\fZ【" + pc.getName() + "】丟棄違法道具！(刪除違法道具)"); 
			return; 
			} 
//			 add end
			if (item.isEquipped()) {
				// \f1你不能夠放棄此樣物品。
				pc.sendPackets(new S_ServerMessage(125));
				return;
			}
			if (item.getBless() >= 128) { // 封印的裝備
				// \f1%0%d是不可轉移的…
				pc.sendPackets(new S_ServerMessage(210, item.getItem()
						.getName()));
				return;
			}

			// 交易紀錄
			if (Config.writeDropLog)
				LogRecorder.writeDropLog(pc, item);

			pc.getInventory().tradeItem(item, count,
					L1World.getInstance().getInventory(x, y, pc.getMapId()));
			pc.turnOnOffLight();
		}
	}

	@Override
	public String getType() {
		return C_DROP_ITEM;
	}
}
